package cn.edu.gzccc.moviecms.controller;



import cn.edu.gzccc.moviecms.domain.Movie;
import cn.edu.gzccc.moviecms.repository.MovieRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 杨友奇
 * @Time 2018/4/21
 * 功能：对电影进行新增，详情，编辑跟删除
 */
@Controller
@RequestMapping(value = "/movie")
public class MovieController {
    @Autowired
    private MovieRepository movieRepository;

    //主页
    @RequestMapping(value = "/index")
    public String index(Model model){
        List<Movie> list = movieRepository.findAll();
        model.addAttribute("list",list);
        return "movie/index";
    }

    //添加电影
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String toAdd(){
        return "movie/add";
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String doAdd(@RequestParam String name,
                        @RequestParam String photo,
                        @RequestParam String createDate){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(createDate);
        } catch (Exception e) {
        }
        Movie movie = new Movie();
        movie.setName(name);
        movie.setPhoto(photo);
        movie.setCreateDate(date);
        movieRepository.save(movie);
        return "redirect:/movie/index";
    }

    //点击跳转到详情页面
    @RequestMapping(value = "/detail/{id}")
    public String detail(Model model,@PathVariable Long id){
        Movie movie = movieRepository.findOne(id);
        model.addAttribute("info", movie);
        return "movie/detail";
    }

    //点击跳转到编辑页面
    @RequestMapping(value = "edit/{id}",method = RequestMethod.GET)
    public String edit(Model model,@PathVariable Long id){
        Movie movie = movieRepository.findOne(id);
        model.addAttribute("info",movie);
        return "movie/edit";
    }
    @RequestMapping(value = "edit",method = RequestMethod.POST)
    public String edit(@RequestParam String name,
                       @RequestParam String photo,
                       @RequestParam String createDate,
                       @RequestParam Long id){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(createDate);
        } catch (Exception e) {
        }
        Movie movie =movieRepository.findOne(id);
        movie.setName(name);
        movie.setPhoto(photo);
        movie.setCreateDate(date);
        movieRepository.save(movie);
        return "redirect:/movie/index";
    }
    //点击删除
    @RequestMapping(value = "delete/{id}",method = RequestMethod.GET)
    public String delect(@PathVariable Long id){
       movieRepository.delete(id);
       return "redirect:/movie/index";
    }

}
